package definite.Guide.Book.BoundedRangeComponents;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.border.TitledBorder;

public class ReadingFile {

	// field declaration
	private static JButton btnStart;
	private static JTextArea textArea;
	private static JScrollPane scrollPane;
	private static JProgressBar progressBar;
	private static Task task;

	// count line number of file
	public static int countLine() {
		int numOfLine = 0;
		InputStreamReader in = null;
		BufferedReader br = null;
		FileInputStream fis = null;

		try {
			fis = new FileInputStream(new File("data.txt"));
			in = new InputStreamReader(fis);
			br = new BufferedReader(in);

			while (br.readLine() != null) {
				numOfLine++;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		} finally {
			try {
				fis.close();
				in.close();
				br.close();
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		return numOfLine;
	}

	public static int MAX = countLine();

	// SwingWorkder Class
	static class Task extends SwingWorker<Void, Integer> {

		private int current;
		private int target;

		public Task(int curr) {
			this.current = 0;
			this.target = curr;
		}

		@Override
		protected Void doInBackground() throws Exception {
			File file = null;
			FileInputStream fis = null;
			InputStreamReader in = null;
			BufferedReader br = null;

			try {
				file = new File("data.txt");
				fis = new FileInputStream(file);
				in = new InputStreamReader(fis);
				br = new BufferedReader(in);

				String line = "";
				while ((line = br.readLine()) != null && current < target) {
					Thread.sleep(50);
					current++;
					textArea.append(line + "\n");
					publish(current);
				}
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}

			return null;
		}

		@Override
		protected void process(List<Integer> chunks) {
			for (Integer integer : chunks) {
				progressBar.setValue(integer);
			}
		}

		@Override
		protected void done() {
			btnStart.setEnabled(true);
			Toolkit.getDefaultToolkit().beep();
		}

	} // end swingworker class

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				JFrame frame = new JFrame("Reading File Monitor");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				JPanel contentPane = new JPanel();
				contentPane.setLayout(new BorderLayout());

				textArea = new JTextArea();
				textArea.setEditable(false);
				textArea.setLineWrap(true);
				textArea.setWrapStyleWord(true);
				scrollPane =
					new JScrollPane(textArea,
							JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
				contentPane.add(scrollPane, BorderLayout.CENTER);

				TitledBorder border =
					new TitledBorder(null, "Progress Results",
							TitledBorder.TOP, TitledBorder.CENTER, new Font(
									"San-Serif", Font.BOLD, 15), Color.RED);
				contentPane.setBorder(border);

				JPanel north = new JPanel();
				btnStart = new JButton("Start");
				// define action for btnstart
				ActionListener action = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						textArea.setText("");
						btnStart.setEnabled(false);
						task = new Task(MAX);
						task.execute();
					}
				};
				btnStart.addActionListener(action);

				progressBar = new JProgressBar(0, MAX);
				progressBar.setStringPainted(true);
				north.add(btnStart, BorderLayout.WEST);
				north.add(progressBar, BorderLayout.CENTER);
				contentPane.add(north, BorderLayout.NORTH);

				frame.setContentPane(contentPane);
				frame.setLocationRelativeTo(null);
				frame.setSize(500, 300);
				frame.setVisible(true);
			}
		};

		EventQueue.invokeLater(runner);
	}

}
